#include "v_tracking.h"
#include "var.h"
#include <string.h>
#include "config.h"
#include "common.h"
#include "serial.h"
#include "FreeRTOS.h"
#include "task.h"
#include "sim900.h"
void RCC_Config(void)
{
    /* Configure HCLK clock as SysTick clock source. */
  SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
	// enable rcc for gpioA,B,C,D
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); 	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); 
	//enable rcc for usart1,2,3,4,5
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 , ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4 , ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5 , ENABLE);	
	//enable rcc for adc
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); 
	//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); 
	//RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
	
}
/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
* Attention		 : None
*******************************************************************************/
void GPIO_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;  

 /*-------------Init DO -----------------------------------*/
//init do for maintaining power
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = DO_DEV_PWR;
  GPIO_Init(DO_DEV_PWR_GPIO, &GPIO_InitStructure);
////DO for gps 
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DO_GPS_PWR;
//  GPIO_Init(DO_GPS_PWR_GPIO, &GPIO_InitStructure);
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DO_GPS_RESET;
//  GPIO_Init(DO_GPS_RESET_GPIO, &GPIO_InitStructure);	
	//gsm 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = DO_GSM_PWR;
  GPIO_Init(DO_GSM_PWR_GPIO	, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = DO_GSM_PWRKEY;
  GPIO_Init(DO_GSM_PWRKEY_GPIO, &GPIO_InitStructure);	
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = DO_GSM_DTR;
  GPIO_Init(DO_GSM_DTR_GPIO, &GPIO_InitStructure);	
//	//rf_id power
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DO_RFID_PWR;
//  GPIO_Init(DO_RFID_PWR_GPIO, &GPIO_InitStructure);
//		//cam power
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DO_CAM_PWR;
//  GPIO_Init(DO_CAM_PWR_GPIO, &GPIO_InitStructure);
	//sd card

		//enable rcc for spi
	//GPIO_PinRemapConfig(GPIO_Remap_SPI3,ENABLE);

	
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DO_SD_PWR;
//  GPIO_Init(DO_SD_PWR_GPIO, &GPIO_InitStructure);
//	SD_PWR_OFF();
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DO_SD_CS;
//  GPIO_Init(DO_SD_CS_GPIO,&GPIO_InitStructure);	
	//data flash
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DO_DF_CS;
//  GPIO_Init(DO_DF_CS_GPIO,&GPIO_InitStructure);		
	//init DO for indicating status of device
	//gps
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DO_GPS_STATUS;
//  GPIO_Init(DO_GPS_STATUS_GPIO, &GPIO_InitStructure);	
	//gsm
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = DO_GSM_STATUS;
  GPIO_Init(DO_GSM_STATUS_GPIO, &GPIO_InitStructure);	
//	//SD card
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DO_SD_STATUS;
//  GPIO_Init(DO_SD_STATUS_GPIO, &GPIO_InitStructure);	
	//device status
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = DO_DEV_STATUS;
  GPIO_Init(DO_DEV_STATUS_GPIO, &GPIO_InitStructure);
	/*------------------END of DO----------------------*/
	/*-----------------Digital input-------------------*/
//	// DI for button
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DI_BAT_PWR;
//  GPIO_Init(DI_BAT_PWR_GPIO, &GPIO_InitStructure);
	// DI for power on/of GPS, GSM, SD,RFID
	//gsm

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = DI_GSM_RI;
  GPIO_Init(DI_GSM_RI_GPIO, &GPIO_InitStructure);	
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = DI_GSM_STATUS;
  GPIO_Init(DI_GSM_STATUS_GPIO, &GPIO_InitStructure);	

	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = DI_GSM_SIM_DECT;
  GPIO_Init(DI_GSM_SIM_DECT_GPIO, &GPIO_InitStructure);	
	
//	//printer
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DI_PRINTER_SWITCH;
//  GPIO_Init(DI_PRINTER_SWITCH_GPIO, &GPIO_InitStructure);	
//	//rfid
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DI_RFID_OVL;
//  GPIO_Init(DI_RFID_OVL_GPIO,&GPIO_InitStructure);
//		//cam
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DI_CAM_OVL;
//  GPIO_Init(DI_CAM_OVL_GPIO,&GPIO_InitStructure);
//	//sd card
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Pin = DI_SD_DECT;
//  GPIO_Init(DI_SD_DECT_GPIO	,&GPIO_InitStructure);	
	/*------------------END of DI----------------------*/	
	
}
/*******************************************************************************
* Function Name  : USART_Init
* Description    : Configures USART
* Input          : None
* Output         : None
* Return         : None
* Attention		 : None
*******************************************************************************/
void USART_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	// Use COM Printer to debug  
  GPIO_InitStructure.GPIO_Pin = PC_USART_RX;     
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(PC_USART_GPIO, &GPIO_InitStructure);	
	
  GPIO_InitStructure.GPIO_Pin = PC_USART_TX;      
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  GPIO_Init(PC_USART_GPIO, &GPIO_InitStructure);

	
  USART_InitStructure.USART_BaudRate = PC_USART_BAUDRATE;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(PC_USART, &USART_InitStructure); 

//	// UART4 for GPS
//  GPIO_InitStructure.GPIO_Pin = GPS_USART_RX;     
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
//  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_Init(GPS_USART_GPIO, &GPIO_InitStructure);	
//	
//  GPIO_InitStructure.GPIO_Pin = GPS_USART_TX;      
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
//  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
//  GPIO_Init(GPS_USART_GPIO, &GPIO_InitStructure);

//	
//  USART_InitStructure.USART_BaudRate = GPS_USART_BAUDRATE;
//  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
//  USART_InitStructure.USART_StopBits = USART_StopBits_1;
//  USART_InitStructure.USART_Parity = USART_Parity_No;
//  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//	USART_Init(GPS_USART, &USART_InitStructure); 
//	USART_ITConfig(GPS_USART,USART_IT_RXNE,ENABLE);
	// UART for GSM
  GPIO_InitStructure.GPIO_Pin = GSM_USART_RX;     
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GSM_USART_RX_GPIO, &GPIO_InitStructure);	
	
  GPIO_InitStructure.GPIO_Pin = GSM_USART_TX;      
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  GPIO_Init(GSM_USART_TX_GPIO, &GPIO_InitStructure);

	
  USART_InitStructure.USART_BaudRate = GSM_USART_BAUDRATE;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(GSM_USART, &USART_InitStructure); 
	USART_ITConfig(GSM_USART,USART_IT_RXNE,ENABLE);
	//usart2 for rfid
//  GPIO_InitStructure.GPIO_Pin = RFID_USART_RX;     
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
//  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_Init(RFID_USART_GPIO, &GPIO_InitStructure);	
//	
//  GPIO_InitStructure.GPIO_Pin = RFID_USART_TX;      
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
//  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
//  GPIO_Init(RFID_USART_GPIO, &GPIO_InitStructure);

//	
//  USART_InitStructure.USART_BaudRate = GSM_USART_BAUDRATE;
//  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
//  USART_InitStructure.USART_StopBits = USART_StopBits_1;
//  USART_InitStructure.USART_Parity = USART_Parity_No;
//  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//	USART_Init(RFID_USART, &USART_InitStructure); 		
//	
//// usart1 for camera
//  GPIO_InitStructure.GPIO_Pin = CAM_USART_RX;     
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
//  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_Init(CAM_USART_GPIO, &GPIO_InitStructure);	
//	
//  GPIO_InitStructure.GPIO_Pin = CAM_USART_TX;      
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
//  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
//  GPIO_Init(CAM_USART_GPIO, &GPIO_InitStructure);

//	GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
//	
//  USART_InitStructure.USART_BaudRate = CAM_USART_BAUDRATE;
//  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
//  USART_InitStructure.USART_StopBits = USART_StopBits_1;
//  USART_InitStructure.USART_Parity = USART_Parity_No;
//  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//	USART_Init(CAM_USART, &USART_InitStructure); 
	
	USART_Cmd(PC_USART, ENABLE);	
  //USART_Cmd(CAM_USART, ENABLE);		
//	USART_Cmd(RFID_USART, ENABLE);	
//	USART_Cmd(GPS_USART, ENABLE);	
	USART_Cmd(GSM_USART, ENABLE);	
	
	//set GPS_reset high, if this pin is low, chip GPS is not running
	//GPS_RESET_HIGH();
}
//void ADC_Config(void)
//{
//	ADC_InitTypeDef ADC_InitStructure;
//  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO, ENABLE);
//}
void NVIC_Config(void)
{
	NVIC_InitTypeDef NVIC_InitStructure; 

//  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);  													
//  NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;   
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;   
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;	      
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//  NVIC_Init(&NVIC_InitStructure);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);  													
  NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;   
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;   
  NVIC_InitStructure.NVIC_IRQChannelSubPriority	 = 0;	      
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
	
//	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);  													
//  NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;   
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4;   
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;	      
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//  NVIC_Init(&NVIC_InitStructure);
	
//	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);  													
//  NVIC_InitStructure.NVIC_IRQChannel = EXTI_PRINTER_SW_IRQn;   
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;   
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;	      
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//  NVIC_Init(&NVIC_InitStructure);
//	
//	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);  													
//  NVIC_InitStructure.NVIC_IRQChannel = EXTI_BAT_PWR_IRQn;   
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4;   
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;	      
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//  NVIC_Init(&NVIC_InitStructure);

//	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);  													
//  NVIC_InitStructure.NVIC_IRQChannel = EXTI_DI_GSM_RI_IRQn;   
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;   
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;	      
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//  NVIC_Init(&NVIC_InitStructure);
}

void TIM_Config(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6 , ENABLE);
  TIM_DeInit(TIM6);
  TIM_TimeBaseStructure.TIM_Period= 200 - 1;		 					
  TIM_TimeBaseStructure.TIM_Prescaler= (36000 - 1);				   
  TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; 	
  TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; 		
  TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
  TIM_ClearFlag(TIM6, TIM_FLAG_Update);							  
  TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE);
  TIM_Cmd(TIM6, ENABLE);										
}
void EXTI_Config(void)
{
//  EXTI_InitTypeDef EXTI_InitStructure;
//	
//  //GPIO_EXTILineConfig(EXTI_BAT_PWR_PORT, EXTI_BAT_PWR_Pin);
//  //GPIO_EXTILineConfig(EXTI_PRINTER_SW_PORT, EXTI_PRINTER_SW_Pin);
//  GPIO_EXTILineConfig(EXTI_DI_GSM_RI_PORT, EXTI_DI_GSM_RI_Pin);
//	
////  EXTI_ClearITPendingBit(EXTI_BAT_PWR_Line);
////  EXTI_ClearITPendingBit(EXTI_PRINTER_SW_Line);
//	EXTI_ClearITPendingBit(EXTI_DI_GSM_RI_Line);
//	
//  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
//  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
//  //EXTI_InitStructure.EXTI_Line = EXTI_BAT_PWR_Line | EXTI_PRINTER_SW_Line|EXTI_DI_GSM_RI_Line;
//  EXTI_InitStructure.EXTI_Line = EXTI_DI_GSM_RI_Line;
//  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
//  EXTI_Init(&EXTI_InitStructure);

}
// void SPI_Config1(void)
//{
//	GPIO_InitTypeDef GPIO_InitStructure;
//	SPI_InitTypeDef SPI_InitStructure;
//	//SPI_InitTypeDef SPI_InitStructure;
//	/* Thiet lap Clock cho GPIO, SPI */
//	RCC_APB2PeriphClockCmd(SD_DF_SPI_GPIO_CLK|RCC_APB2Periph_AFIO, ENABLE); 
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); 
//	/* Thiet lap GPIO de chay voi chuc nang SPI */
//	/* Pin12 as CS */
//		//GPIO_PinRemapConfig(GPIO_Remap_SPI1,ENABLE);
//		//enable rcc for spi
//	GPIO_PinRemapConfig(GPIO_Remap_SPI1,ENABLE);
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
//	
////	GPIO_InitStructure.GPIO_Pin = DO_SD_CS;
////	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
////	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
////	GPIO_Init(DO_SD_CS_GPIO, &GPIO_InitStructure);

//	/* Pin13 as SCK Pin15 as MOSI */
//	GPIO_InitStructure.GPIO_Pin = SD_DF_SPI_SCK|SD_DF_SPI_MOSI;
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_Init(SD_DF_SPI_GPIO, &GPIO_InitStructure);
//	/* Pin14 as MISO */
//	GPIO_InitStructure.GPIO_Pin = SD_DF_SPI_MISO;
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_Init(SD_DF_SPI_GPIO, &GPIO_InitStructure);
//	
//	
//	RCC_APB1PeriphClockCmd(SD_DF_SPI_CLOCK	, ENABLE); 
//	
//  SPI_InitStructure.SPI_Mode = SPI_Mode_Master ;	
//	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
//	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
//	SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;				//mode 0 lam viec phu hop voi MMC/SD
//	SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge ;
//	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
//	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; // baud = 72M/256 = 281k
//	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
//	SPI_InitStructure.SPI_CRCPolynomial = 7;	
//	
//	SPI_Init(SD_DF_SPI, &SPI_InitStructure);
//	SPI_Cmd(SD_DF_SPI,ENABLE);
//}

void vInitHardware(void *arg)
{

  RCC_Config();
	GPIO_Config();
  GSM_LED_OFF();
	USART_Config();
	PWR_HOLD_ON();
	NVIC_Config();
	EXTI_Config();
	USART_SendString(PC_USART,"V-Tracking V1.0\r\n");
	USART_SendString(PC_USART,"Phong Thiet Bi Thong Minh - Trung tam tu van & trien khai - VTICT\r\n");  
  GSM_PWR_ON();
	vTaskDelay(500);
	GSM_PWRKEY_ON();
	vTaskDelay(500);
	GSM_PWRKEY_OFF();
	vTaskDelay(2000);
	GSM_PWRKEY_ON();  
  while(GPIO_ReadInputDataBit(DI_GSM_STATUS_GPIO,DI_GSM_STATUS) == RESET);
  USART_SendString(PC_USART,"Init Completed!\r\n");
  GSM_LED_ON();
  vTaskDelay(3000);
  xTaskCreate( vGSMTask, ( const char * ) NULL , 200, (void *)&sim_cmd, 3 , NULL ); //task thuc hien lenh phai co uu tien cao hon task add lenh de dam bao lenh duoc thuc hien xong moi tiep tuc add tiep
  xTaskCreate( vProcessNewSMS, ( const char * ) NULL , 200, NULL, 2 , NULL );
  xTaskCreate( vProcessIncommingCall, ( const char * ) NULL , 200, NULL, 2 , NULL );

  if(GSM_AddTask(&sim_cmd,"ATE1\r",SendCmd,1000,1) == CMD_OK)
  {
   USART_SendString(PC_USART,"Su dung Echo\r\n");
  }
  else
  {
    USART_SendString(PC_USART,"Echo Command that bai\r\n");
  }
  if(GSM_AddTask(&sim_cmd,"AT+CNMI=2,1,0,0\r",SendCmd,1000,1) == CMD_OK)
  {
    USART_SendString(PC_USART,"Su dung chi thi tin nhan den\r\n");
  }
  else
  {
    USART_SendString(PC_USART,"AT+CNMI Command that bai\r\n");
  }  
  if(GSM_AddTask(&sim_cmd,"AT+CLIP=1\r",SendCmd,1000,1) == CMD_OK)
  {
    USART_SendString(PC_USART,"AT+CLIP=1 thuc hien thanh cong\r\n");
  }
  else
  {
    USART_SendString(PC_USART,"AT+CLIP=1 Command that bai\r\n");
  }    
  for(;;)
  {

//    if(GSM_AddTask("AT\r",SendCmd,"\r\nOK",2000,1,2) == 0)
//    {
//      USART_SendString(PC_USART,"Task1: AT: OK\r\n");
//    }
//    else
//      USART_SendString(PC_USART,"Task1: Command that bai!\r\n");
//    vTaskDelay(2000);
//    if(GSM_AddTask("AT+IPR=9600\r",SendCmd,"\r\nOK",2000,1,2) == 0)
//      USART_SendString(PC_USART,"Task1: AT+IPR: OK\r\n");
//    else
//      USART_SendString(PC_USART,"Task1: Command that bai!\r\n");
  }
}
